package com.bzxhh.sync;

import java.util.concurrent.locks.ReentrantLock;

//可重入锁 又称 递归锁
public class SyncLockDemo {

    public synchronized void add() {
        add();
    }

    public static void main(String[] args) {

        //Lock演示可重入锁
        ReentrantLock lock = new ReentrantLock();
        //创建线程
        new Thread(() -> {
            try {
                //上锁
                lock.lock();
                System.out.println(Thread.currentThread().getName() + " 内层");

                try {
                    //上锁
                    lock.lock();
                    System.out.println(Thread.currentThread().getName() + " 外层");
                } finally {
//                    //解锁
//                    lock.unlock();
                }
            } finally {
                //解锁
                lock.unlock();
            }
        }, "t1").start();

        //创建新线程
        new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " 层");
            lock.unlock();
        }, "t2").start();

//        new SyncLockDemo().add();

//        //synchronized
//        Object o = new Object();
//        new Thread(() -> {
//            synchronized (o) {
//                System.out.println(Thread.currentThread().getName() + " 外层");
//
//                synchronized (o) {
//                    System.out.println(Thread.currentThread().getName() + " 中层");
//
//                    synchronized (o) {
//                        System.out.println(Thread.currentThread().getName() + " 内层");
//                    }
//                }
//            }
//        }, "t1").start();
    }

}
